<?php
/**
 * 代理中心-会员管理
 *
 *
 *
 * @copyright  Copyright (c) 2014-2015 TuntronKJ Inc. (http://www.tuntron.com)
 * @license    http://www.tuntron.com
 * @link       http://www.tuntron.com
 * @since      File available since Release v1.1
 */
defined('InTuntronKJ') or exit('Access Invalid!');
class MemberManageControl extends BaseTeamControl{
	
	//会员管理
	public function indexOp(){
		$user_model = Model('member');
		$uid = $_SESSION['uid'];
		$userinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		
		$param = array();

		$nickname = $_GET['QueryName'];
		$type = $_GET['type']?$_GET['type']:0;
		$param['nickname'] = $nickname;
		$param['type'] = $type;

		$page = $_GET['page']?$_GET['page']:1;
		$start = 15*$page-15;
		
		//直属下级会员UID
		$parent_list = $user_model->getMemberList(array('parentId'=>$uid));
		if(!empty($parent_list) && is_array($parent_list)){
			foreach($parent_list as $item){
				$parent_uid[] = $item['uid'];
			}
		}
	
		if($nickname){	
			$members = $user_model->getMemberInfo(array('username'=>$nickname));
			$param['type'] = 0;
			if($uid == $members['uid']){
				$user_list[] = $members;
			}else{
				$parent_arr = explode(',', $members['parents']);
				if($members && in_array($userinfo['uid'],$parent_arr)){
					$user_list = $user_model->getMemberList(array('uid'=>$members['uid']));
				}
			}		
		}else{			
			if($type == 0){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
				$list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($list as $k => $t){
					$list = $this->getUserList($t['uid'],$list);
				}
				$user_list = array_merge($user_list,$list);
			}elseif($type == 1){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
			}elseif($type == 2){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
			}elseif($type == 3){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($user_list as $k => $t){
					$user_list = $this->getUserList($t['uid'],$user_list);
				}
			}
		}

		$uids = "";
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $key => $value){
				$uids = $uids.$value['uid'].',';
			}
			$uids = substr($uids,0,strlen($uids)-1);
			$condition['uid'] = array('in', $uids);
			$user_list = $user_model->getMemberList($condition,'','',$start.',15','uid asc');

			$user_count = $user_model->getMemberCount($condition);
			$totalpage = ceil($user_count/15);	
			foreach($user_list as $e => $v){
				$session_list = $user_model->getSessionList(array('uid'=>$v['uid']));
				$user_list[$e]['isOnLine'] = $session_list[0]['isOnLine'];
				$user_list[$e]['accessTime'] = $session_list[0]['accessTime'];
			}	
		}
		if(!$user_count){
			$user_count = 0;
			$totalpage = 1;
		}

		Tpl::output('user_list',$user_list);
		Tpl::output('parent_uid',$parent_uid);
		Tpl::output('userinfo',$userinfo);
		Tpl::output('param',$param);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$user_count);
		Tpl::showpage('membermanage');
	}

	//查看下级会员
	public function searchMemberLowerOp(){
		$user_model = Model('member');

		$username = $_GET['QueryName'];
		$page = $_GET['page']?$_GET['page']:1;
		$start = 15*$page-15;
		
		$members = $user_model->getMemberInfo(array('username'=>$username));

		//直属下级会员UID
		$parent_list = $user_model->getMemberList(array('parentId'=>$members['uid']));
		if(!empty($parent_list) && is_array($parent_list)){
			foreach($parent_list as $item){
				$parent_uid[] = $item['uid'];
			}
		}
				
		$condition['parentId'] = $members['uid'];
		$user_list = $user_model->getMemberList($condition,'','',$start.',15','uid asc');
		$user_count = $user_model->getMemberCount($condition);
		if($user_count){
			if($user_count%15 == 0){
				$totalpage = $user_count/15;
			}else{
				$totalpage = intval($user_count/15)+1;
			}
		}else{
			$user_count = 0;
			$totalpage = 1;
		}		
		
		Tpl::output('username',$username);
		Tpl::output('user_list',$user_list);
		Tpl::output('parent_uid',$parent_uid);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$user_count);
		Tpl::output('search',1);
		Tpl::showpage('membermanage');
		
		
	}
	
	//添加会员
	public function AddMemberOp(){		
		$urlshang = $_SERVER['HTTP_REFERER']; //上一页URL
		$urldan = $_SERVER['SERVER_NAME']; //本站域名
		$urlcheck=substr($urlshang,7,strlen($urldan));
		if($urlcheck<>$urldan){
			Re_json_encode('-100','数据包被篡改，请重新操作!');
		}		
		if(!$_POST) Re_json_encode('-100','参数出错!');
		
		$user_model = Model('member');	
		$param_model = Model('params');
		$userinfo = $user_model->getMemberInfo(array('uid'=>$_SESSION['uid']));
		$paraminfo = $param_model->getParamsInfo(array('name'=>'fanDianDiff'));			//上下级最小返点差
		
		
		$param['username'] = $_POST['userName'];
		$param['fanDian'] = floatval($_POST['returnPoint']);
		$param['fanDianBdw'] = floatval($_POST['returnNotPoint']);
		$param['type'] = $_POST['userType']=='会员'?0:1;
		$param['password'] = $_POST['userPassword'];
		$param['parentId']= $userinfo['uid'];
		$param['parents']= $userinfo['parents'];
		$param['password']=md5($param['password']);
		$param['regIP']= getIp();
		$param['regTime']= time();

		if(!$_POST['nickname']) $param['nickname']=$param['username'];
		if(!$_POST['name']) $param['name']=$param['username'];
		

		$rule = "/^[A-Za-z\x{4e00}-\x{9fa5}][A-Za-z0-9\x{4e00}-\x{9fa5}]{1,19}$/u";			//用户名正则检验
		preg_match($rule, $param['username'],$result);
		$passlen=strlen($_POST['userPassword']);

		//接收参数检查
		if(!$param['username']) Re_json_encode('error','用户名不能为空!');
		if($param['fanDian']<0) Re_json_encode('error','返点不能小于0!');
		if($param['fanDianBdw']<0) Re_json_encode('error','不定位返点不能小于0!');
		
		if($param['fanDian'] > ($userinfo['fanDian'] -$paraminfo['value']))
			Re_json_encode('error',"返点不能大于".($userinfo['fanDian'] -$paraminfo['value'])."!");
		if($param['fanDianBdw'] > 0 && $param['fanDianBdw'] > ($userinfo['fanDianBdw'] -$paraminfo['value']))
			Re_json_encode('error',"不定位返点不能大于".($userinfo['fanDianBdw'] -$paraminfo['value'])."!");
		if($param['fanDian']){
			if(($param['fanDian']*100) % ($paraminfo['value']*100)){
				Re_json_encode('error',sprintf('返点只能是%.1f的倍数', $paraminfo['value']));
			}
		}
		
		if($param['type']!=0 && $param['type']!=1) Re_json_encode('error','会员类型出错!');
		if($passlen<6 || $passlen>16) Re_json_encode('error','密码长度不合法!');
		if(!$result[0]) Re_json_encode('error','用户名不合法，请重新填写!');
		
		$user_model->beginTransaction();
		try{
			$member = $user_model->getMemberInfo(array('username'=>$param['username']));
			if(!empty($member) && is_array($member)) Re_json_encode('error','用户名已被使用!');

			$id = $user_model->addMember($param);
			if($id){
				$parents = $userinfo['parents'].','.$id;
				$user_model->updateMember(array('parents'=>$parents),$id);
				$user_model->commit();
				Re_json_encode('success','添加会员成功!');
			}else{
				Re_json_encode('error','添加会员失败!');
			}					
		}catch(Exception $e){
			$user_model->rollBack();
			Re_json_encode('error','添加会员失败!');
		}
	}
	
	//更新会员
	public function UpdateMemberOp(){
		$user_model = Model('member');
		$param_model = Model('params');
		$uid = $_SESSION['uid'];
		
		$param['username'] = $_POST['editUserName'];
		$param['fanDian'] = floatval($_POST['editReturnPoint']);
		$param['fanDianBdw'] = floatval($_POST['editReturnNotPoint']);
				
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		$userinfo = $user_model->getMemberInfo(array('username'=>$param['username']));
		$paraminfo = $param_model->getParamsInfo(array('name'=>'fanDianDiff'));
		
		if(!$userinfo) Re_json_encode('error','此会员不存在!');
		if($userinfo['parentId'] != $uid) Re_json_encode('error','此会员不是你的直属下线，无法修改!');
		if($userinfo['uid'] == $uid) Re_json_encode('error','不能修改自己的返点!');
		
		if($param['fanDian']<$userinfo['fanDian']) 
			Re_json_encode('error',"修改后的返点必须大于".$userinfo['fanDian']."!");
		if($param['fanDianBdw']<$userinfo['fanDianBdw']) 
			Re_json_encode('error',"修改后的不定位返点必须大于".$userinfo['fanDianBdw']."!");
		if($param['fanDian'] > ($memberinfo['fanDian'] -$paraminfo['value']))
			Re_json_encode('error',"修改后的返点必须小于或等于".($memberinfo['fanDian'] -$paraminfo['value'])."!");
		if($param['fanDianBdw'] > ($memberinfo['fanDianBdw'] -$paraminfo['value']))
			Re_json_encode('error',"修改后的不定位返点必须小于或等于".($memberinfo['fanDianBdw'] -$paraminfo['value'])."!");
		if($param['fanDian']){
			if(($param['fanDian']*100) % ($paraminfo['value']*100))
				Re_json_encode('error',sprintf('返点只能是%.1f的倍数', $paraminfo['value']));
		}
		
		$update = $user_model->updateMember($param,$userinfo['uid']);
		if($update){
			Re_json_encode('success','会员修改成功!');
		}else{
			Re_json_encode('error','提交信息有误,请刷新页面重新提交!');
		}
	}
	
	//团队游戏记录
	public function TeamGamerecordOp(){
		$bets_model = Model('bat');
		$user_model = Model('member');
		$model_play = Model("played");
		$model_data = Model('data_class');
		
		$param = array();
		$uid = $_SESSION['uid'];
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		$param['username'] = $memberinfo['username'];
		
		$page = $_GET['pageIndex']?$_GET['pageIndex']:1;
		$start = 15*$page-15;
		
		$fromTime = strtotime($_GET['start']);
		$toTime = strtotime($_GET['end']);
		$nickname = $_GET['keyword'];
		$type = $_GET['type'];					//用户类型：0-所有人，1-自己，2-直属下线，3-所有下线
		$param['nickname'] = $nickname;
		$param['type'] = $type;
		
		if($fromTime && $toTime){
			$fromTime = $fromTime+3600*3;
			$toTime = $toTime+3600*3;
		}else{
			$today_zero = strtotime('today');
			$time = time();
			$fromTime = $today_zero+3600*3;			//查询起始时间
			if($fromTime > $time){
				$toTime = $fromTime;				//查询结束时间
				$fromTime = $today_zero-3600*21;
			}else{
				$toTime = $fromTime+3600*24;
			}
		}
		$param['beginDate'] = date('Y-m-d',$fromTime);
		$param['endDate'] = date("Y-m-d",$toTime);
		$condition['actionTime'] = array('between',$fromTime.','.$toTime);
				
		if($nickname){			
			$user_info = $user_model->getMemberInfo(array('username'=>$nickname));
			$param['type'] = 0;
			if($uid == $members['uid']){
				$user_list[] = $members;
			}else{
				$parent_arr = explode(',', $user_info['parents']);
				if($user_info && in_array($uid,$parent_arr)){
					$user_list = $user_model->getMemberList(array('uid'=>$user_info['uid']));
				}
			}
		}else{
			if($type == 0){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
				$list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($list as $k => $t){
					$list = $this->getUserList($t['uid'],$list);
				}
				$user_list = array_merge($user_list,$list);
			}elseif($type == 1){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
			}elseif($type == 2){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
			}elseif($type == 3){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($user_list as $k => $t){
					$user_list = $this->getUserList($t['uid'],$user_list);
				}
			}
		}
		$uids = "";
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $key => $value){
				$uids = $uids.$value['uid'].',';
			}
			$uids = substr($uids,0,strlen($uids)-1);
			$condition['uid'] = array('in', $uids);
			$condition['isDelete'] = 0;
			$bet_list= $bets_model->getBetList($condition,'','',$start.',15','id desc');
			if(!empty($bet_list) && is_array($bet_list)){
				foreach($bet_list as $key=>$value){
					$playinfo = $model_play->getPlayInfo(array('id'=>$value['playedId']));
					$bet_list[$key]['LotteryName'] = '重庆彩';
					$bet_list[$key]['RuleName'] = $playinfo['name'];
				}
			}
			
			$bet_count = $bets_model->getBetCount($condition);
			$totalpage = ceil($bet_count/15);
		}
		if(!$bet_count){			
			$bet_count = 0;
			$totalpage = 1;
		}		
		
		$data_zj['totalPages'] = $totalpage;
		$data_zj['totalRecords'] = $bet_count;
		foreach($bet_list as $key=>$value){
			$playinfo = $model_play->getPlayInfo(array('id'=>$value['playedId']));
		
			$data_zj['rows'][$key]['RowNumber'] = $key+1;
			$data_zj['rows'][$key]['BetContent'] = $value['actionData'];						//投注号码
			$data_zj['rows'][$key]['BonusNumber'] = $value['lotteryNo'];						//开奖号码
			$data_zj['rows'][$key]['InsertDate'] = date('Y-m-d H:i:s', $value['actionTime']);	//投注时间
			$data_zj['rows'][$key]['UserName'] = $value['username'];							//用户名
			$data_zj['rows'][$key]['UserType'] = $member_info['type']==0?'会员':'代理';			//用户类型
			$data_zj['rows'][$key]['RuleName'] = $playinfo['name'];								//玩法
			$data_zj['rows'][$key]['LotteryName'] = '重庆时时彩';								//彩种
			$data_zj['rows'][$key]['Id'] = $value['id'];
			$data_zj['rows'][$key]['Uid'] = $value['uid'];										//投注用户UID
			$data_zj['rows'][$key]['MemberId'] = $uid;											//当前用户ID
			$data_zj['rows'][$key]['LotteryId'] = 1;											//彩种ID
			$data_zj['rows'][$key]['IssueNumber'] = $value['actionNo'];							//期号
			$data_zj['rows'][$key]['BetRuleId'] = $value['playedId'];
			$data_zj['rows'][$key]['BetUnitPrice'] = $value['mode'];
			$data_zj['rows'][$key]['UnitPaymentPrice'] = $value['bonusProp'];					//赔率
			$data_zj['rows'][$key]['ReturnPoint'] = $item['fanDian'];							//返点
			$data_zj['rows'][$key]['OrderDate'] = "/Date(".$value['actionTime'].")/";
			$data_zj['rows'][$key]['OrderStatus'] = $v['isDelete']?10:0;
			$data_zj['rows'][$key]['TotalBet'] = $value['actionNum']*$value['beiShu'];
			$data_zj['rows'][$key]['OrderGuid'] = $value['serializeId'];
			$data_zj['rows'][$key]['BonusTotalNumber'] = $value['zjCount'];
			$data_zj['rows'][$key]['Description'] = '重庆时时彩 '.$value['actionNo'].'期 '.$group[0]['groupName'];		//说明
			$data_zj['rows'][$key]['profit'] = number_format(($value['bonus']-$value['beiShu']*$value['actionNum']*$value['mode']),2);									//盈亏
			//是否开奖
			$kjdata = $model_data->getDataList(array('number'=>$value['actionNo'],'type'=>1));
			if($kjdata){
				$data_zj['rows'][$key]['hasRunLottery'] = "是";
			}else{
				$data_zj['rows'][$key]['hasRunLottery'] = "否";
			}
			$data_zj['rows'][$key]['BonusMoney'] = $value['bonus'];
			$data_zj['rows'][$key]['EncodeId'] = $value['wjorderId'];
			$data_zj['lotteryNumberDict']['1,'.$value['actionNo']] = $value['lotteryNo'];
		}
		$json = json_encode($data_zj);

		Tpl::output('json',$json);
		Tpl::output('bet_list',$bet_list);
		Tpl::output('param',$param);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$bet_count);
		Tpl::showpage('teamgamerecord');
	}
	
	//团队盈亏报表
	public function TeamRprofitlossOp(){
		
		$user_model = Model('member');
		
		$param = array();
		$uid = $_SESSION['uid'];
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$_SESSION['uid']));		
		$param['username'] = $memberinfo['username'];
		
		$nickname = $_GET['keyword'];
		$fromTime = strtotime($_GET['start']);
		$toTime = strtotime($_GET['end']);
		$type = $_GET['type']?$_GET['type']:0;
		$param['nickname'] = $nickname;
		$param['type'] = $type;		
		
		$page = $_GET['pageIndex']?$_GET['pageIndex']:1;
		$start = 15*$page-15;
		
		if($fromTime && $toTime){
			$fromTime = $fromTime+3600*3;
			$toTime = $toTime+3600*3;
		}else{
			$today_zero = strtotime('today');
			$time = time();
			$fromTime = $today_zero+3600*3;			//查询起始时间
			if($fromTime > $time){
				$toTime = $fromTime;				//查询结束时间
				$fromTime = $today_zero-3600*21;
			}else{
				$toTime = $fromTime+3600*24;
			}
		}	
		$param['beginDate'] = date('Y-m-d',$fromTime);
		$param['endDate'] = date("Y-m-d",$toTime);
		
		$betTimeWhere="and actionTime between $fromTime and $toTime";
		$cashTimeWhere="and c.actionTime between $fromTime and $toTime";
		$rechargeTimeWhere="and r.actionTime between $fromTime and $toTime";
		$fanDiaTimeWhere="and actionTime between $fromTime and $toTime";
		$fanDiaTimeWhere2="and l.actionTime between $fromTime and $toTime";
		$brokerageTimeWhere=$fanDiaTimeWhere2;
		
		if($nickname){
			$members = $user_model->getMemberInfo(array('username'=>$nickname));
			$param['type'] = 0;
			if($uid == $members['uid']){
				$user_list[] = $members;
			}else{
				$parent_arr = explode(',', $members['parents']);
				if($members && in_array($memberinfo['uid'],$parent_arr)){
					$user_list = $user_model->getMemberList(array('uid'=>$members['uid']));
				}
			}			
		}else{
			if($type == 0){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
				$list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($list as $k => $t){
					$list = $this->getUserList($t['uid'],$list);
				}
				$user_list = array_merge($user_list,$list);
			}elseif($type == 1){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
			}elseif($type == 2){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
			}elseif($type == 3){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($user_list as $k => $t){
					$user_list = $this->getUserList($t['uid'],$user_list);
				}
			}

		}
		$uids = "";
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $key => $value){
				$uids = $uids.$value['uid'].',';
			}
			$uids = substr($uids,0,strlen($uids)-1);
			$condition['uid'] = array('in', $uids);
			$user_list = $user_model->getMemberList($condition,'','',$start.',15','uid asc');
			$user_count = $user_model->getMemberCount($condition);
			if($user_count%15 == 0){
				$totalpage = $user_count/15;
			}else{
				$totalpage = intval($user_count/15)+1;
			}

			foreach($user_list as $key => $value){
				$uid = $value['uid'];
				$data['uid'] = $uid;
				$file = "username,coin,uid,parentId,(select sum(b.mode * b.beiShu * b.actionNum) from ssc_bets b where b.`uid`= $uid and isDelete=0 $betTimeWhere) betAmount, (select sum(b.bonus) from ssc_bets b where b.`uid`= $uid $betTimeWhere) zjAmount,(select sum(c.amount) from ssc_member_cash c where c.`uid`= $uid and c.state=0 $cashTimeWhere) cashAmount,(select sum(r.amount) from ssc_member_recharge r where r.`uid`= $uid and r.state in(1,2,9) $rechargeTimeWhere) rechargeAmount,(select sum(l.coin) from ssc_coin_log l where l.`uid`= $uid and l.liqType in(2,3) $fanDiaTimeWhere) fanDianAmount";
				$member_list[] = $user_model->getMemberReportInfo($data,$file);
			}
		}
		if(!$user_count){
			$user_count = 0;
			$totalpage = 1;
		}
		
		Tpl::output('member_list',$member_list);
		Tpl::output('param',$param);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$user_count);
		Tpl::showpage('teamprofitloss');
	}

	//团队统计
	public function TeamCoinAllOp(){
		
		$user_model = Model('member');
		$param = array();
		$uid = $_SESSION['uid'];
		
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		
		$param['type'] = $memberinfo['type'];
		$param['username'] = $memberinfo['username'];
		$param['amount'] = $memberinfo['coin'];
				
		
		//所有下级会员
		$user_list = $user_model->getMemberList(array('parentId'=>$uid));
		$param['user_count'] = count($user_list);
		
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $k => $t){
				$user_list = $this->getUserList($t['uid'],$user_list);
			}

			$count_amount = 0;
			foreach($user_list as $value){
				$count_amount += $value['coin'];
			}

			$param['all_amount'] = $count_amount+$memberinfo['coin'];
			$param['all_user_count'] = count($user_list);
			
		}else{
			$param['all_amount'] = $memberinfo['coin'];
			$param['all_user_count'] = 0;
		}

		Tpl::output('param',$param);
		Tpl::showpage('teamcoinall');
	}
	
	//团队账变记录
	public function TeamCoinOp(){
		
		$bets_model = Model('bat');
		$user_model = Model('member');

		$param = array();
		$uid = $_SESSION['uid'];
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		$param['username'] = $memberinfo['username'];
		
		$page = $_GET['pageIndex']?$_GET['pageIndex']:1;
		$start = 15*$page-15;
		
		$fromTime = strtotime($_GET['start']);
		$toTime = strtotime($_GET['end']);
		$nickname = $_GET['keyword'];
		$type = $_GET['type']?$_GET['type']:0;
		$liqType = $_GET['liqtype']?$_GET['liqtype']:0;
		
		$param['type'] = $type;
		$param['liqType'] = $liqType;
		
		if($fromTime && $toTime){
			$fromTime = $fromTime+3600*3;
			$toTime = $toTime+3600*3;
		}else{
			$today_zero = strtotime('today');
			$time = time();
			$fromTime = $today_zero+3600*3;			//查询起始时间
			if($fromTime > $time){
				$toTime = $fromTime;				//查询结束时间
				$fromTime = $today_zero-3600*21;

			}else{
				$toTime = $fromTime+3600*24;

			}
		}
		$param['beginDate'] = date('Y-m-d',$fromTime);
		$param['endDate'] = date("Y-m-d",$toTime);
		$condition['actionTime'] = array('between',$fromTime.','.$toTime);
		
		if($liqType == 1){
			//账户充值
			$condition['liqType'] = array('in',1);
		}elseif($liqType == 2){
			//账户提现
			$condition['liqType'] = array('in',101);
		}elseif($liqType == 3){
			//投注返点
			$condition['liqType'] = array('in',2);
		}elseif($liqType == 4){
			//游戏投注
			$condition['liqType'] = array('in',102);
		}elseif($liqType == 5){
			//撤单返款
			$condition['liqType'] = array('in',7);
		}elseif($liqType == 6){
			//奖金派送
			$condition['liqType'] = array('in',6);
		}elseif($liqType == 7){
			//佣金活动
			$condition['liqType'] = array('in',1);
		}
		
		if($nickname){
			$members = $user_model->getMemberInfo(array('username'=>$nickname));
			$param['type'] = 0;
			if($uid == $members['uid']){
				$user_list[] = $members;
			}else{
				$parent_arr = explode(',', $members['parents']);
				if($members && in_array($memberinfo['uid'],$parent_arr)){
					$user_list = $user_model->getMemberList(array('uid'=>$members['uid']));
				}
			}
		}else{
			if($type == 0){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
				$list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($list as $k => $t){
					$list = $this->getUserList($t['uid'],$list);
				}
				$user_list = array_merge($user_list,$list);
			}elseif($type == 1){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
			}elseif($type == 2){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
			}elseif($type == 3){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($user_list as $k => $t){
					$user_list = $this->getUserList($t['uid'],$user_list);
				}
			}
		}
		$uids = "";
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $key => $value){
				$uids = $uids.$value['uid'].',';
			}
			$uids = substr($uids,0,strlen($uids)-1);
			$condition['uid'] = array('in', $uids);
			
			$profit_list = $bets_model->getCoinLogList($condition,'','',$start.',15');
			if(!empty($profit_list) && is_array($profit_list)){
				foreach($profit_list as $key=>$value){
					$userinfo = $user_model->getMemberInfo(array('uid'=>$value['uid']));
					$profit_list[$key]['username'] = $userinfo['username'];
				}
			}
			
			$profit_count = $bets_model->getCoinLogCount($condition);
			$totalpage = ceil($profit_count/15);
		}
		if(!$profit_count){
			$profit_count = 0;
			$totalpage = 1;
		}
		if(!empty($profit_list) && is_array($profit_list)){
			foreach($profit_list as $key => $value){
				$extfiled = explode('|', $value['extfield1']);
				$profit_list[$key]['lotteryName'] = $extfiled[0];
				$profit_list[$key]['ruleName'] = $extfiled[1];
				$profit_list[$key]['issueNumber'] = $extfiled[2];
			}
		}
		

		Tpl::output('profit_list',$profit_list);
		Tpl::output('param',$param);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$profit_count);
		Tpl::showpage('teamcoin');
	}
	
	//团队充值记录
	public function TeamRechargeOp(){
		
		$user_model = Model('member');
		$cash_model = Model('cash');
		$bank_model = Model('bank');
		
		$uid = $_SESSION['uid'];
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		
		$page = $_GET['pageIndex']?$_GET['pageIndex']:1;
		$start = 15*$page-15;
		
		$fromTime = strtotime($_GET['start']);
		$toTime = strtotime($_GET['end']);
		$nickname = $_GET['keyword'];
		$type = $_GET['type']?$_GET['type']:0;
		$param['nickname'] = $nickname;
		$param['type'] = $type;
				
		if($fromTime && $toTime){
			$fromTime = $fromTime+3600*3;
			$toTime = $toTime+3600*3;
		}else{
			$today_zero = strtotime('today');
			$time = time();
			$fromTime = $today_zero+3600*3;			//查询起始时间
			if($fromTime > $time){
				$toTime = $fromTime;				//查询结束时间
				$fromTime = $today_zero-3600*21;
			}else{
				$toTime = $fromTime+3600*24;
			}
		}
		$param['beginDate'] = date('Y-m-d',$fromTime);
		$param['endDate'] = date("Y-m-d",$toTime);
		$condition['actionTime'] = array('between',$fromTime.','.$toTime);
		
		if($nickname){		
			$members = $user_model->getMemberInfo(array('username'=>$nickname));
			$param['type'] = 0;
			if($uid == $members['uid']){
				$user_list[] = $members;
			}else{
				$parent_arr = explode(',', $members['parents']);
				if($members && in_array($memberinfo['uid'],$parent_arr)){
					$user_list = $user_model->getMemberList(array('uid'=>$members['uid']));
				}
			}		
		}else{
			if($type == 0){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
				$list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($list as $k => $t){
					$list = $this->getUserList($t['uid'],$list);
				}
				$user_list = array_merge($user_list,$list);
			}elseif($type == 1){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
			}elseif($type == 2){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
			}elseif($type == 3){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($user_list as $k => $t){
					$user_list = $this->getUserList($t['uid'],$user_list);	
				}
			}
		}
		$uids = "";
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $key => $value){
				$uids = $uids.$value['uid'].',';
			}
			$uids = substr($uids,0,strlen($uids)-1);
			$condition['uid'] = array('in', $uids);
			
			$recharge_list = $cash_model->getRechargeList($condition,'','',$start.',15','id asc');
			if(!empty($recharge_list) && is_array($recharge_list)){
				foreach($recharge_list as $k => $v){
					$userinfo = $user_model->getMemberInfo(array('uid'=>$v['uid']));
					$bankinfo = $bank_model->getBankInfo(array('id'=>$v['mBankId']));
			
					$recharge_list[$k]['nickname'] = $userinfo['username'];
					$recharge_list[$k]['bankname'] = $bankinfo['name'];
					$recharge_list[$k]['type'] = $userinfo['type'];
				}
			}
			
			$recharge_count = $cash_model->getRechargeCount($condition);
			if($recharge_count%10 == 0){
				$totalpage = $recharge_count/15;
			}else{
				$totalpage = intval($recharge_count/15)+1;
			}
		}
		if(!$recharge_count){
			$recharge_count = 0;
			$totalpage = 1;
		}

		Tpl::output('recharge_list',$recharge_list);
		Tpl::output('param',$param);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$recharge_count);
		Tpl::showpage('teamrecharge');
	}
	
	//团队提现记录
	public function TeamCashRecordOp(){
		
		$user_model = Model('member');
		$cash_model = Model('cash');
		$bank_model = Model('bank');
		
		$uid = $_SESSION['uid'];
		$param = array();
		
		$page = $_GET['pageIndex']?$_GET['pageIndex']:1;
		$start = 15*$page-15;
		
		$fromTime = strtotime($_GET['start']);	
		$toTime = strtotime($_GET['end']);
		$nickname = $_GET['keyword'];
		$type = $_GET['type']?$_GET['type']:0;					//用户类型：0-所有人，1-自己，2-直属下线，3-所有下线
		$param['nickname'] = $nickname;
		$param['type'] = $type;
		
		if($fromTime && $toTime){
			$fromTime = $fromTime+3600*3;
			$toTime = $toTime+3600*3;
		}else{
			$today_zero = strtotime('today');
			$time = time();
			$fromTime = $today_zero+3600*3;			//查询起始时间
			if($fromTime > $time){
				$toTime = $fromTime;				//查询结束时间
				$fromTime = $today_zero-3600*21;
			}else{
				$toTime = $fromTime+3600*24;
			}
		}	
		$param['beginDate'] = date('Y-m-d',$fromTime);
		$param['endDate'] = date("Y-m-d",$toTime);
		$condition['actionTime'] = array('between',$fromTime.','.$toTime);
		
		if($nickname){
			$members = $user_model->getMemberInfo(array('username'=>$nickname));
			$param['type'] = 0;
			if($uid == $members['uid']){
				$user_list[] = $members;
			}else{
				$parent_arr = explode(',', $members['parents']);
				if($members && in_array($uid,$parent_arr)){
					$user_list = $user_model->getMemberList(array('uid'=>$members['uid']));
				}
			}					
		}else{
			if($type == 0){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
				$list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($list as $k => $t){
					$list = $this->getUserList($t['uid'],$list);
				}
				$user_list = array_merge($user_list,$list);
			}elseif($type == 1){
				$user_list = $user_model->getMemberList(array('uid'=>$uid));
			}elseif($type == 2){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
			}elseif($type == 3){
				$user_list = $user_model->getMemberList(array('parentId'=>$uid));
				foreach($user_list as $k => $t){
					$user_list = $this->getUserList($t['uid'],$user_list);	
				}
			}
		}
		$uids = "";
		if(!empty($user_list) && is_array($user_list)){
			foreach($user_list as $key => $value){
				$uids = $uids.$value['uid'].',';
			}
			$uids = substr($uids,0,strlen($uids)-1);
			$condition['uid'] = array('in', $uids);
			$cash_list = $cash_model->getCashList($condition,'','',$start.',15','id asc');
			
			if(!empty($cash_list) && is_array($cash_list)){
				foreach($cash_list as $k => $v){
					$userinfo = $user_model->getMemberInfo(array('uid'=>$v['uid']));
					$bankinfo = $bank_model->getBankInfo(array('id'=>$v['bankId']));
			
					$cash_list[$k]['nickname'] = $userinfo['username'];
					$cash_list[$k]['bankname'] = $bankinfo['name'];
					$cash_list[$k]['type'] = $userinfo['type'];
				}
			}
			
			$cash_count = $cash_model->getCashCount($condition);
			if($cash_count%10 == 0){
				$totalpage = $cash_count/15;
			}else{
				$totalpage = intval($cash_count/15)+1;
			}
		}
		if(!$cash_count){
			$cash_count = 0;
			$totalpage = 1;
		}

		Tpl::output('cash_list',$cash_list);
		Tpl::output('param',$param);
		Tpl::output('page',$page);
		Tpl::output('totalpage',$totalpage);
		Tpl::output('count',$cash_count);
		Tpl::showpage('teamcashrecord');
	}
	
	//用户推广链接
	public function LinksOp(){
		$user_model = Model('member');
		$link_model = Model('link');		
		$uid = $_SESSION['uid'];
		
		$userinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		$user_links = $link_model->getMemberLinks(array('uid'=>$uid));
		
		Tpl::output('user_links',$user_links);
		Tpl::output('userinfo',$userinfo);
		Tpl::showpage('teamlinks');
	}
	
	//添加推广链接
	public function insertLinkOp(){
		$user_model = Model('member');
		$link_model = Model('link');
		$param_model = Model('params');
		$uid = $_SESSION['uid'];
		
		$memberinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		$paraminfo = $param_model->getParamsInfo(array('name'=>'fanDianDiff'));

		$update['type'] = $_POST['userType'];
		$update['fanDian'] = floatval($_POST['genlinkReturnPoint']);
		$update['fanDianBdw'] = floatval($_POST['genlinkNotReturnPoint']);
		$update['regIP'] = getIp();
		$update['regTime'] = time();
		$update['uid'] = $uid;
		$update['updateTime'] = date('Y:m:d H:i:s');

		if($update['fanDian'] < 0)	Re_json_encode('error','返点不能小于0!');
		if($update['fanDianBdw'] < 0) Re_json_encode('error','不定位返点不能小于0!');
		if($update['type']!=0 && $update['type']!=1) Re_json_encode('error','类型出错，请重新操作!');
		if($update['fanDian'] > ($memberinfo['fanDian'] -$paraminfo['value']))
			Re_json_encode('error',"返点不能大于或等于".($memberinfo['fanDian'] -$paraminfo['value'])."!");
		if($update['fanDianBdw'] > ($memberinfo['fanDianBdw'] -$paraminfo['value']))
			Re_json_encode('error',"不定位返点不能大于或等于".($memberinfo['fanDian'] -$paraminfo['value'])."!");
		
		//检查返点设置
		if($update['fanDian']){
			if(($update['fanDian']*100) % ($paraminfo['value']*100))
				Re_json_encode('error',sprintf('返点只能是%.1f的倍数', $paraminfo['value']));				
		}else{
			$update['fanDian']=0.0;
		}
		
		$link_model->beginTransaction();
		try{
			$user_links = $link_model->getMemberLinks(array('uid'=>$uid,'type'=>$update['type']));
							
			if($user_links) Re_json_encode('error',"此链接已经存在!");
			
			$lid = $link_model->addMemberLinks($update);
			if($lid){
				$link_model->commit();
				Re_json_encode('success',"推广链接添加成功!");
			}else{
				Re_json_encode('error',"推广链接添加失败!");
			}		
		}catch(Exception $e){
			$link_model->rollBack();
			Re_json_encode('error',"推广链接添加失败!原因:未知信息!!");
		}
	}
	
	
	//编辑推广链接
	public function editLinkOp(){
		$user_model = Model('member');
		$link_model = Model('link');
		$param_model = Model('params');
		$uid = $_SESSION['uid'];
		
		$lid = intval($_POST['editLinkId']);
		$userinfo = $user_model->getMemberInfo(array('uid'=>$uid));
		$userlink = $link_model->getLinkInfo(array('lid'=>$lid));
		$paraminfo = $param_model->getParamsInfo(array('name'=>'fanDianDiff'));
		
		$update['fanDian'] = floatval($_POST['editlinkReturnPoint']);
		$update['fanDianBdw'] = floatval($_POST['editlinkNotReturnPoint']);		
		
		if($update['fanDian'] < 0)	Re_json_encode('error','返点不能小于0!');
		if($update['fanDianBdw'] < 0) Re_json_encode('error','不定位返点不能小于0!');
		if($update['type']!=0 && $update['type']!=1) Re_json_encode('error','类型出错，请重新操作!');
		if($update['fanDian'] > ($userinfo['fanDian'] -$paraminfo['value']))
			Re_json_encode('error',"返点不能大于或等于".($userinfo['fanDian'] -$paraminfo['value'])."!");
		if($update['fanDianBdw'] > ($userinfo['fanDianBdw'] -$paraminfo['value']))
			Re_json_encode('error',"不定位返点不能大于或等于".($userinfo['fanDian'] -$paraminfo['value'])."!");
		
		//检查返点设置
		if($update['fanDian']){
			if(($update['fanDian']*100) % ($paraminfo['value']*100))
				Re_json_encode('error',sprintf('返点只能是%.1f的倍数', $paraminfo['value']));
		}else{
			$update['fanDian']=0.0;
		}
		
		if(!$userlink) Re_json_encode('error','此注册链接不存在!');
		if($userlink['uid'] != $uid) Re_json_encode('error','只能修改自己的推广链接!');
		
		$update = $link_model->updateMemberLinks($update,$lid);
		if($update){
			Re_json_encode('success','注册链接修改成功!');
		}else{
			Re_json_encode('error','提交信息有误,请刷新页面重新提交!');
		}
	}

	//获取推广链接
	public function searchLinkOp(){
		$user_model = Model('member');
		$link_model = Model('link');
		$uid = $_SESSION['uid'];
		
		$lid = intval($_POST['searchLinkId']);
		$userlink = $link_model->getLinkInfo(array('lid'=>$lid));
		
		if(!empty($userlink) && is_array($userlink)){
			$userinfo = $user_model->getMemberInfo(array('uid'=>$userlink['uid'])); 
			//将用户名与链接lid进行合并加密
			$data = encrypt($userinfo['username'].'|'.$lid);
			$data = "http://218.244.144.36/kjcx/index.php?ac=login&op=register&token=".$data;
// 			$data = "http://localhost/jin008/index.php?ac=login&op=register&token=".$data;
			Re_json_encode('success',$data);
		}else{
			Re_json_encode('error','此注册链接不存在!');
		}
		
	}
	
	//删除推广链接
	public function delLinkOp(){
		$user_model = Model('member');
		$link_model = Model('link');
		$uid = $_SESSION['uid'];
		
		$lid = intval($_POST['delLinkId']);
		$userlink = $link_model->getLinkInfo(array('lid'=>$lid));
		
		if(!empty($userlink) && is_array($userlink)){
			if($userlink['uid'] != $uid){
				Re_json_encode('error','只能删除自己的推广链接!');
			}else{
				$del = $link_model->delMemberLinks($lid);
				if($del){
					Re_json_encode('success','推广链接删除成功!');
				}else{
					Re_json_encode('error','未知错误!');
				}
			}
		}else{
			Re_json_encode('error','此注册链接不存在!');
		}
		
	}
	
	//用户下线
	public function getUserList($uid,$list){
		$user_model = Model('member');
		$user_all_list = array();
		$user_all_list = array_merge($user_all_list,$list);
		$user_list = $user_model->getMemberList(array('parentId'=>$uid));
		if(!empty($user_list) && is_array($user_list)){
			$user_all_list = array_merge($user_all_list,$user_list);			
			foreach($user_list as $key => $value){
				self::getUserList($value['uid'],$user_all_list);
			}
		}
		return $user_all_list;
		
		
	}
	
}
